//
//  MyTabInfoViewCollectionView.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2020/9/10.
//  Copyright © 2020 moon. All rights reserved.
//

#import "MyTabInfoViewCollectionView.h"
#import "MenuCollectionViewCellModel.h"
@interface MyTabInfoViewCollectionView(){
    BOOL _isDelayLoading;
}
@end
@implementation MyTabInfoViewCollectionView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.tabBarItem.title = @"Collection";
        
        [self.showMoreButton addTarget:self action:@selector(__moreButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
        
        self.collectionView = [[MKUICollectionFlowLayoutView alloc] initWithFrame:CGRectZero];
        self.collectionView.collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        self.collectionView.collectionViewFlowLayout.itemSize = CGSizeMake(60, 60);
        self.collectionView.collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.collectionView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.collectionView];
        self.flowlayout.items = @[self.showMoreButton,self.collectionView];
    }
    return self;
}
- (void)layoutSubviews{
    CGRect oldBounds = self.collectionView.bounds;
    [super layoutSubviews];
    if(!CGSizeEqualToSize(oldBounds.size, self.collectionView.bounds.size)){
        [self.collectionView.model reloadCollectionViewData];
    }
}
- (void)__moreButtonDidTap:(id)sender{
    self.cellModel[@"collection_showMore"] = @(![self.cellModel mk_boolForKeyPath:@"collection_showMore" otherwise:NO]);
    [self.cellModel refreshWithAnimated:YES];
}
- (void)reloadData{
    [super reloadData];
    self.showMoreButton.selected = [self.cellModel mk_boolForKeyPath:@"collection_showMore" otherwise:NO];
    [self __reloadCollectionData];
}
- (void)__reloadCollectionData{
    [self.collectionView.model removeAllSectionModels];
    MyMenuInfo *info = self.cellModel.modelValue;
    NSArray<Menu *> *histMenus = info.delayLoadedMenus;
    if(![self.cellModel mk_boolForKeyPath:@"collection_showMore" otherwise:NO]){
        histMenus = [histMenus subarrayWithRange:NSMakeRange(0, 2)];
    }
    for (Menu *menu in histMenus) {
        MenuCollectionViewCellModel *cm = [[MenuCollectionViewCellModel alloc] initWithModelObject:menu];
        [self.collectionView.model addCellModel:cm];
    }
    [self.collectionView.model reloadCollectionViewData];
}
- (void)tabBarContentViewWillAppear:(BOOL)animated{
    [super tabBarContentViewWillAppear:animated];
    if(_isDelayLoading){
        return;
    }
    MyMenuInfo *info = self.cellModel.modelValue;
    if(!info.delayLoadedMenus){
        _isDelayLoading = YES;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self->_isDelayLoading = NO;
            info.delayLoadedMenus = [Menu sharedMenus];
            [self.cellModel refreshWithAnimated:YES];
        });
    }else{
        [self reloadData];
    }
}
- (void)dealloc{
    
}
@end
